package com.neutech.maplestory.entity;

/**
 * 小怪
 */

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;
import java.util.List;

public class Mob extends AbstractMapleStoryObject{
    public static Image[] imgs = new Image[100];
    static {
        for (int i = 0; i < 6; i++) {
            imgs[i] = ImageUtil.getImage("mob01_right_stand" + i);
        }
        for (int i =6; i < 12; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_stand" + (i - 6));
        }
        for (int i = 12; i < 15; i++) {
            imgs[i] = ImageUtil.getImage("mob01_right_walk" + (i - 12));
        }
        for (int i = 15; i < 18; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_walk" + (i - 15));
        }
        for (int i = 18; i < 28; i++) {
            imgs[i] = ImageUtil.getImage("mob01_right_die" + (i - 18));
        }
        for (int i = 28; i < 38; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_die" + (i - 28));
        }
        imgs[28] = ImageUtil.getImage("mob_right_hit");
        imgs[29] = ImageUtil.getImage("mob_left_hit");
    }
    /**
     * 最大血量
     */
    public int MAX_HP;
    public Mob(){
        this.width = imgs[0].getWidth(null);
        this.height = imgs[0].getHeight(null);
        this.dir = Direction.LEFT;
        this.action = Action.WALK;
        this.live = true;
        this.MAX_HP = 5000;
        this.HP = MAX_HP;
    }
    public Mob(MapleStoryClient msc,int x, int y,int speed){
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }
    private int count;
    private int step;
    private int dieStep;
    @Override
    public void draw(Graphics g) {
        if (dieStep >= 10){
            this.live = false;
            dieStep = 0;
        }
        if (!live){
            msc.mobs.remove(this);
            msc.hero.LEVEL += 1;
            msc.hero.MAX_HP += 100;
            msc.hero.MAX_MP += 100;
            msc.hero.ATT += 50;
            return;
        }
        switch (dir){
            case RIGHT:
                switch (action){
                    case STAND:
                        // right stand
                        if (count++ % 3 == 0){
                            g.drawImage(imgs[step++ % 6],x,y + 27,null);
                        }else {
                            g.drawImage(imgs[step % 6],x,y + 27,null);
                        }
                        break;
                    case WALK:
                        // right walk
                        if (count++ % 3 == 0){
                            g.drawImage(imgs[step++ % 3 + 12],x,y + 27,null);
                        }else {
                            g.drawImage(imgs[step % 3 + 12],x,y + 27,null);
                        }
                        break;
                    case HIT:
                        // right hit
                        g.drawImage(imgs[28],x,y + 35,null);
                        this.action = Action.WALK;
                        break;
                    case DIE:
                        // right die
                        if (count++ % 3 == 0){
                            g.drawImage(imgs[dieStep++ % 11 + 18],x,y + 35,null);
                        }else {
                            g.drawImage(imgs[dieStep % 11 + 18],x,y + 35,null);
                        }
                        break;
                    default:
                        break;
                }
                break;
            case LEFT:
                switch (action){
                    case STAND:
                        // left stand
                        if (count++ % 3 == 0){
                            g.drawImage(imgs[step++ % 6 + 6],x,y + 27,null);
                        }else {
                            g.drawImage(imgs[step % 6 + 6],x,y + 27,null);
                        }
                        break;
                    case WALK:
                        // left walk
                        if (count++ % 3 == 0){
                            g.drawImage(imgs[step++ % 3 + 15],x,y + 27,null);
                        }else {
                            g.drawImage(imgs[step % 3 + 15],x,y + 27,null);
                        }
                        break;
                    case HIT:
                        // left hit
                        g.drawImage(imgs[29],x,y + 35,null);
                        this.action = Action.WALK;
                        break;
                    case DIE:
                        // left die
                        if (count++ % 3 == 0){
                            g.drawImage(imgs[dieStep++ % 11 + 28],x,y + 35,null);
                        }else {
                            g.drawImage(imgs[dieStep % 11 + 28],x,y + 35,null);
                        }
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;

        }
        if (HP < MAX_HP){
            bloodBar.draw(g);
        }
        move();
    }

    @Override
    public void move() {
        switch (dir){
            case RIGHT:
                switch (action){
                    case WALK:
                        this.x += speed;
                        break;
                    default:
                        break;
                }
                break;
            case LEFT:
                switch (action){
                    case WALK:
                        this.x -= speed;
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }

        outOfBound();

    }

    public BloodBar bloodBar = new BloodBar();
    class BloodBar extends AbstractMapleStoryObject{
        private Image img;
        public BloodBar(){
            this.img = ImageUtil.getImage("blood");
            this.width = img.getWidth(null);
        }
        @Override
        public void draw(Graphics g) {
            /*// 画血条边框
            // 当内部类访问外部类相同属性名时，使用外部类.this.属性名
            Color c = g.getColor();
            g.drawRect(Mob.this.x,Mob.this.y - 15,Mob.this.width,10);
            // 填充血框
            g.setColor(Color.GREEN);
            g.fillRect(Mob.this.x + 1,Mob.this.y - 15 + 1, (int) (Mob.this.width * Mob.this.HP / Mob.this.MAX_HP),10 - 1);
            g.setColor(c);*/
            int n = Mob.this.width / this.width * Mob.this.HP / Mob.this.MAX_HP;
            for (int i = 0; i < n; i++) {
                g.drawImage(img,Mob.this.x + (i * this.width ),Mob.this.y - 15,null);
            }
        }
    }

    /**
     * 怪物出界问题
     */
    private void outOfBound(){
        if (this.x <= 0){
            this.dir = Direction.RIGHT;
        }
        if (this.x >= Constant.GAME_WIDTH - this.width){
            this.dir = Direction.LEFT;
        }

        if (x >= 0 && x <= 740){
            if (this.x <= 180 && y >= 646){
                this.dir = Direction.RIGHT;
            }
            if (this.x >= 740 - this.width && y >= 646){
                this.dir = Direction.LEFT;
            }
        }
        if (x >= 1015 && x <= 1415){
            if (this.x <= 1015 && y >= 646){
                this.dir = Direction.RIGHT;
            }
            if (this.x >= 1415 - this.width && y >= 646){
                this.dir = Direction.LEFT;
            }
        }
        if (x >= 665 && x <= 1345 - this.width){
            if (this.x <= 665 && this.y <= 96){
                this.dir = Direction.RIGHT;
            }
            if (x >= 1340 - this.width && this.y <= 96){
                this.dir = Direction.LEFT;
            }
        }
        if (x <= 1090 - this.width){
            if (x >= 1080 - this.width && this.y >= 284){
                this.dir = Direction.LEFT;
            }
        }
    }
    public boolean hit(Hero hero){
        if (this.live && this.getRectangle().intersects(hero.getRectangle())){
            if (count++ % 3 == 0){
                hero.HP -= 10;
                this.action = Action.HIT;

            }

            return true;
        }
        return false;
    }
}
